home *** CD-ROM | disk | FTP | other *** search
/ Shareware Grab Bag / Shareware Grab Bag.iso / 001 / pibt40s2.arc / KREMCOMM.MOD < prev    next >
Text File  |  1987-09-30  |  13KB  |  376 lines

  1. (*----------------------------------------------------------------------*)
  2. (*     Kermit_Remote_Commands --- Finish server mode transfers          *)
  3. (*----------------------------------------------------------------------*)
  4.  
  5. PROCEDURE Kermit_Remote_Commands(     Remote_Command : AnyStr;
  6.                                   VAR Do_A_Receive   : BOOLEAN );
  7.  
  8. (*----------------------------------------------------------------------*)
  9. (*                                                                      *)
  10. (*     Procedure:  Kermit_Remote_Commands                               *)
  11. (*                                                                      *)
  12. (*     Purpose:    Issues commands to remote server                     *)
  13. (*                                                                      *)
  14. (*     Calling Sequence:                                                *)
  15. (*                                                                      *)
  16. (*        Kermit_Remote_Commands(     Remote_Command : AnyStr;          *)
  17. (*                                VAR Do_A_Receive   : BOOLEAN );       *)
  18. (*                                                                      *)
  19. (*           Remote_Command --- Server command to execute               *)
  20. (*           Do_A_Receive   --- TRUE on exit if file transfer needs     *)
  21. (*                              to be executed.                         *)
  22. (*                                                                      *)
  23. (*----------------------------------------------------------------------*)
  24.  
  25. VAR
  26.    Try                : INTEGER;
  27.    Ch                 : CHAR;
  28.    Command_Menu       : Menu_Type;
  29.    Command            : INTEGER;
  30.    Param_S1           : AnyStr;
  31.    Param_S2           : AnyStr;
  32.    Quit               : BOOLEAN;
  33.    Ack_OK             : BOOLEAN;
  34.    Save_Display       : BOOLEAN;
  35.    Local_Save         : Saved_Screen_Ptr;
  36.    Packet_Buffer_Data : AnyStr;
  37.  
  38. CONST
  39.    Quit_Item           = 11;
  40.    Max_Remote_Commands = 10;
  41.  
  42. (* STRUCTURED *) CONST             (* Long names for display *)
  43.  
  44.    Remote_Command_Names: ARRAY[1..Max_Remote_Commands] OF STRING[16] =
  45.                          ( 'Change Directory',
  46.                            'Delete File',
  47.                            'Directory',
  48.                            'Finish Server',
  49.                            'Help',
  50.                            'Host',
  51.                            'Logout Server',
  52.                            'Space',
  53.                            'Type',
  54.                            'Who' );
  55.  
  56.                                    (* Short names for parsing *)
  57.  
  58.    Remote_Command_Short_Names: ARRAY[1..Max_Remote_Commands] OF STRING[9] =
  59.                                ( 'CWD',
  60.                                  'DELETE',
  61.                                  'DIRECTORY',
  62.                                  'FINISH',
  63.                                  'HELP',
  64.                                  'HOST',
  65.                                  'LOGOUT',
  66.                                  'SPACE',
  67.                                  'TYPE',
  68.                                  'WHO' );
  69.  
  70.                                    (* # params for each command *)
  71.  
  72.    KParams : ARRAY[1..Max_Remote_Commands] OF BYTE =
  73.              ( 2, 1, 1, 0, 1, 1, 0, 1, 1, 2 );
  74.  
  75.                                    (* Command letters each command *)
  76.  
  77.    KLetters: ARRAY[1..Max_Remote_Commands] OF STRING[2] =
  78.              ( 'GC', 'GE', 'GD', 'GF', 'GH', 'C', 'GL', 'GU', 'GT', 'GW' );
  79.  
  80. (*----------------------------------------------------------------------*)
  81. (*     Check_Remote_Return --- Check returned packet from remote        *)
  82. (*----------------------------------------------------------------------*)
  83.  
  84. PROCEDURE Check_Remote_Return;
  85.  
  86. BEGIN (* Check_Remote_Return *)
  87.                                    (* Pick up a packet           *)
  88.    Receive_Packet;
  89.  
  90.    IF Packet_OK AND ( NOT Kermit_Abort ) THEN
  91.       BEGIN
  92.                                    (* Check if ACK or NAK packet received. *)
  93.                                    (* May also be error packet.            *)
  94.  
  95.          CASE Kermit_Packet_Type OF
  96.  
  97.                                    (* Make sure ACK is for correct block *)
  98.  
  99.             ACK_Pack :   IF ( Rec_Packet_Num = ( Packet_Num MOD 64 ) ) THEN
  100.                             BEGIN
  101.                                ACK_OK := TRUE;
  102.                                IF ( Rec_Packet_Length > 0 ) THEN
  103.                                   BEGIN
  104.                                      WRITELN;
  105.                                      Write_Log('Remote Kermit replies: ' +
  106.                                                COPY( Rec_Packet_Ptr^[1], 1,
  107.                                                      Rec_Packet_Length ) ,
  108.                                                FALSE, TRUE );
  109.                                   END;
  110.                                DELAY( Two_Second_Delay );
  111.                             END;
  112.  
  113.                                    (* Error packet sent *)
  114.             Error_Pack : BEGIN
  115.                             WRITELN;
  116.                             Write_Log('Error from remote Kermit: ' +
  117.                                        COPY( Rec_Packet_Ptr^[1], 1, Rec_Packet_Length ),
  118.                                        FALSE, TRUE );
  119.                             DELAY( Two_Second_Delay );
  120.                          END;
  121.  
  122.              Send_Pack : BEGIN
  123.                             Ack_OK := TRUE;
  124.                          END;
  125.                                    (* Something else -- don't ACK it *)
  126.             ELSE
  127.                ACK_OK := FALSE;
  128.  
  129.          END (* CASE *)
  130.  
  131.       END
  132.    ELSE
  133.       ACK_OK := FALSE;
  134.  
  135. END   (* Check_Remote_Return *);
  136.  
  137. (*----------------------------------------------------------------------*)
  138. (*     Get_Remote_Command --- Prompt for remote command to execute      *)
  139. (*----------------------------------------------------------------------*)
  140.  
  141. PROCEDURE Get_Remote_Command;
  142.  
  143. BEGIN (* Get_Remote_Command *)
  144.                                    (* Save current screen *)
  145.  
  146.    Save_Screen( Local_Save );
  147.                                    (* Display menu of remote commands *)
  148.  
  149.    Make_And_Display_Menu( Command_Menu, Quit_Item, 6, 20, 0, 0, Quit_Item,
  150.                           'Remote Kermit commands: ',
  151.                           'Change Directory;Delete File;Directory;Finish;Help;Host;Logout;Space;Type;Who;Quit;',
  152.                           TRUE, TRUE, Command );
  153.  
  154.    Quit := FALSE;
  155.  
  156.    IF ( Command > 0 ) AND ( Command < Quit_Item ) THEN
  157.       BEGIN
  158.  
  159.          Draw_Menu_Frame( 15, 5, 75, 11, Menu_Frame_Color, Menu_Title_Color,
  160.                           Menu_Text_Color, 'Kermit ' +
  161.                           Remote_Command_Names[Command] );
  162.  
  163.          Window( 16, 6, 74, 10 );
  164.  
  165.          GoToXY( 1 , 1 );
  166.  
  167.          Packet_Buffer_Data := KLetters[ Command ];
  168.  
  169.       END
  170.    ELSE
  171.       Quit := TRUE;
  172.  
  173.    IF ( ( KParams[Command] > 0 ) AND ( NOT Quit ) ) THEN
  174.       BEGIN
  175.  
  176.          Param_S1           := '';
  177.          Param_S2           := '';
  178.  
  179.          CASE Command OF
  180.             1 : WRITELN(' Enter directory specification: ');
  181.             2 : WRITELN(' Enter name of file to delete:  ');
  182.             3 : WRITELN(' Enter directory specification: ');
  183.             5 : WRITELN(' Enter topic to get help about: ');
  184.             6 : WRITELN(' Enter host command:            ');
  185.             8 : WRITELN(' Enter area:                    ');
  186.             9 : WRITELN(' Enter file to type:            ');
  187.             10: WRITELN(' Enter userid:                  ');
  188.          END (* CASE *);
  189.  
  190.          WRITE('> ');
  191.          Read_Edited_String( Param_S1 );
  192.          WRITELN;
  193.  
  194.          IF ( LENGTH( Param_S1 ) > 0 ) THEN
  195.             Quit := ( Param_S1[1] = CHR( ESC ) );
  196.  
  197.          IF ( ( KParams[Command] > 1 ) AND ( NOT Quit ) ) THEN
  198.             BEGIN
  199.  
  200.                CASE Command OF
  201.                   1 : WRITELN(' Enter password:                ');
  202.                   10: WRITELN(' Enter options:                 ');
  203.                   ELSE;
  204.                END (* CASE *);
  205.  
  206.                WRITE('> ');
  207.                Read_Edited_String( Param_S2 );
  208.                WRITELN;
  209.  
  210.                IF ( LENGTH( Param_S2 ) > 0 ) THEN
  211.                   Quit := ( Param_S2[1] = CHR( ESC ) );
  212.  
  213.             END;
  214.  
  215.       END (* NOT Quit *);
  216.                                    (* Remove selection window       *)
  217.    Restore_Screen( Local_Save );
  218.    Reset_Global_Colors;
  219.  
  220. END   (* Get_Remote_Command *);
  221.  
  222. (*----------------------------------------------------------------------*)
  223. (*     Parse_Remote_Command --- Parse remote command to execute         *)
  224. (*----------------------------------------------------------------------*)
  225.  
  226. PROCEDURE Parse_Remote_Command;
  227.  
  228. VAR
  229.    IPos  : INTEGER;
  230.    CName : ShortStr;
  231.    I     : INTEGER;
  232.    L     : INTEGER;
  233.  
  234.  
  235. BEGIN (* Parse_Remote_Command *)
  236.  
  237.    Remote_Command := Substr( Remote_Command, 2, LENGTH( Remote_Command ) - 1 );
  238.    L              := LENGTH( Remote_Command );
  239.    Param_S1       := '';
  240.    Param_S2       := '';
  241.  
  242.    IPos  := POS( ' ' , Remote_Command );
  243.  
  244.    IF ( IPos > 0 ) THEN
  245.       BEGIN
  246.          CName          := Substr( Remote_Command , 1 , IPos - 1 );
  247.          Remote_Command := Trim( Substr( Remote_Command , IPos + 1 , L - IPos ) );
  248.       END
  249.    ELSE
  250.       BEGIN
  251.          CName          := Remote_Command;
  252.          Remote_Command := '';
  253.       END;
  254.  
  255.    CName   := UpperCase( Trim( CName ) );
  256.    Command := 0;
  257.  
  258.    FOR I := 1 TO Max_Remote_Commands DO
  259.       IF ( CName = Remote_Command_Short_Names[I] ) THEN
  260.          Command := I;
  261.  
  262.    IF ( Command = 0 ) THEN
  263.       Quit := TRUE
  264.    ELSE
  265.       BEGIN
  266.  
  267.          Quit := FALSE;
  268.  
  269.          IF ( KParams[Command] = 1 ) THEN
  270.             Param_S1 := Remote_Command
  271.          ELSE
  272.             BEGIN
  273.  
  274.                IPos  := POS( ' ' , Remote_Command );
  275.  
  276.                IF ( IPos = 0 ) THEN
  277.                   Param_S1 := Remote_Command
  278.                ELSE
  279.                   BEGIN
  280.                      Param_S1 := Trim( Substr( Remote_Command , 1 , IPos - 1 ) );
  281.                      Param_S2 := Trim( Substr( Remote_Command, IPos + 1,
  282.                                        LENGTH( Remote_Command ) - IPos ) );
  283.                   END;
  284.  
  285.             END;
  286.  
  287.       END;
  288.  
  289. END   (* Parse_Remote_Command *);
  290.  
  291. (*----------------------------------------------------------------------*)
  292.  
  293. BEGIN (* Kermit_Remote_Commands *)
  294.  
  295.                                    (* If command line not passed in, *)
  296.                                    (* prompt for command.            *)
  297.  
  298.    IF ( LENGTH( Remote_Command ) = 0 ) THEN
  299.       Get_Remote_Command
  300.    ELSE
  301.       Parse_Remote_Command;
  302.                                    (* Send command to remote server  *)
  303.    IF Quit THEN
  304.       Do_A_Receive := FALSE
  305.    ELSE
  306.       BEGIN
  307.                                    (* Construct command packet *)
  308.  
  309.          Packet_Buffer_Data := KLetters[ Command ];
  310.  
  311.          IF ( Command = 6 ) THEN
  312.             Packet_Buffer_Data := Packet_Buffer_Data + Param_S1
  313.          ELSE
  314.             BEGIN
  315.  
  316.                IF ( LENGTH( Param_S1 ) > 0 ) THEN
  317.                   Packet_Buffer_Data := Packet_Buffer_Data +
  318.                                         CHR( LENGTH( Param_S1 ) + 32 ) +
  319.                                         Param_S1;
  320.  
  321.                IF ( LENGTH( Param_S2 ) > 0 ) THEN
  322.                   Packet_Buffer_Data := Packet_Buffer_Data +
  323.                                         CHR( LENGTH( Param_S2 ) + 32 ) +
  324.                                         Param_S2;
  325.  
  326.             END;
  327.  
  328.          Write_Log( 'Remote Kermit command = '    +
  329.                      Remote_Command_Names[Command] + ' ' +
  330.                      Param_S1 + ' ' +
  331.                      Param_S2,
  332.                      FALSE, FALSE );
  333.  
  334.          Packet_Num          := 0;
  335.          Try                 := 0;
  336.          Packets_Received    := 0;
  337.          Packets_Sent        := 0;
  338.          Packets_Bad         := 0;
  339.          Kermit_MaxTry       := 5;
  340.          Kermit_Abort        := FALSE;
  341.          Kermit_Retry        := FALSE;
  342.          Kermit_Abort_Level  := No_Abort;
  343.          Kermit_Screen_Write := FALSE;
  344.          His_Chk_Type        := '1';
  345.  
  346.          Send_Packet_Length  := LENGTH( Packet_Buffer_Data );
  347.  
  348.          MOVE( Packet_Buffer_Data[1], Send_Packet_Ptr^[4], Send_Packet_Length );
  349.  
  350.          Send_Packet_Length  := Send_Packet_Length + 3;
  351.  
  352.          Build_Packet;
  353.  
  354.          Save_Display   := Display_Status;
  355.          Display_Status := FALSE;
  356.  
  357.          REPEAT
  358.  
  359.             Try := Try + 1;
  360.  
  361.             Send_Packet;
  362.  
  363.             Check_Remote_Return;
  364.  
  365.          UNTIL ( Kermit_Abort OR ACK_OK OR ( Try > Kermit_MaxTry ) );
  366.  
  367.          Do_A_Receive := ( Try <= Kermit_MaxTry ) AND
  368.                          ( Kermit_Packet_Type = Send_Pack ) AND
  369.                          ( NOT Kermit_Abort );
  370.  
  371.          Display_Status := Save_Display;
  372.  
  373.      END;
  374.  
  375. END    (* Kermit_Remote_Commands *);
  376.